package com.qingcloud.df.executor.config;

import com.qingcloud.df.sdk.component.logger.CompInstLogHandler;
import com.qingcloud.df.sdk.component.logger.LogHandshakeInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistration;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import java.util.List;


/**
 * 组件参数自动装配
 *
 * @author wangjinnan
 */
@Configuration
@EnableWebSocket
@Slf4j
public class ExecutorConfiguration implements WebMvcConfigurer, WebSocketConfigurer {


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //TODO
    }
    /**
     * @param argumentResolvers 解析类
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        //TODO
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        WebSocketHandlerRegistration registration =  registry.addHandler(compInstLogHandler(),
                "/websocket").addInterceptors(new LogHandshakeInterceptor());
        registration.setAllowedOrigins("*");
    }

    @Bean
    public CompInstLogHandler compInstLogHandler() {
        return new CompInstLogHandler();
    }

}
